/**
 * ajax获取数据
 */
function getAjax() {
    let findname = JSON.parse(localStorage.getItem("findname"))
    $.ajax({
        method: "get",
        url: "http://www.xiongmaoyouxuan.com/api/search",
        data: findname,
        success: res => {
            if (res.code == 200) {
                if (res.data.isEnd) {
                    //更多商品选项消失
                    $(".warnTitle").css({ "display": "block" })
                    $(".morecarts").css({ "display": "none" })
                } else {
                    //更多商品选项出现
                    $(".warnTitle").css({ "display": "none" })
                    $(".morecarts").css({ "display": "block" })
                    console.log(res.data)
                    cartListShow(res.data.list)
                }
            }
        },
        error: res => {
            alert("网络错误" + res.status)
        }
    })
}
/**
 * 搜索商品
 */
function findcart() {
    $(".findinput>form>button").on("click", e => {
        e.preventDefault()
        let data = {
            word: $(".findinput>form>input").val(),
            start: 0,
        }
        localStorage.setItem("findname", JSON.stringify(data))
        getAjax()
    })
}
getAjax()
findcart()
getMore()
backTop()
orderChange()
const cartlists = [];
/**
* 渲染商品
*/
function cartListShow(obj) {
    //获取商品数据
    cartlists.push(...obj);
    let length = cartlists.length;
    while (length % 4 > 0) {
        length--;
    }
    let str3 = ``;
    //判断是否到底
    for (let i = 0; i < length; i++) {
        str3 += `<div class="cartbox" onclick="goodsDetails(${cartlists[i].id})">
      <img src="${cartlists[i].image}">

      <div class="textvalue">
      <p>${cartlists[i].title}</p>
      
      <div class="centerbox">
      <span class="${cartlists[i].platform == 1 ? "taobao" : "tianmao"}">${cartlists[i].platform == 1 ? "淘宝" : "天猫"}</span>
      <span class="baoyou">包邮</span>
      </div>

      <div class="pricebox">
      <span class="pricelogo">￥</span>
      <span class="bigprice">${parseInt(cartlists[i].price)}</span>
      <span class="smartprice">.${((cartlists[i].price * 10) % 10).toFixed(0)}</span>
      <span class="sale-num">${cartlists[i].saleNum}人已买</span>
      <span class="${cartlists[i].couponValue != "" ? "youhui" : ""}">${cartlists[i].couponValue}</span>
      </div>
      </div> 
      </div>`
    }
    $(".cartlists").html(str3)
}
/**
 * 回到顶部
 */

function backTop() {
    //浏览器上下滑动时
    window.onscroll = () => {
        //获取浏览器卷去的距离
        let high = document.body.scrollTop || document.documentElement.scrollTop
        //当卷去高度达到400时显示回到顶部按钮
        if (high > 400) {
            $(".toTop").css("display", "flex");
        } else {
            $(".toTop").css("display", "none");
        }
    }
    //给回到顶部添加点击事件
    $(".toTop").on("click", () => {
        window.scrollTo({
            top: 0,
            left: 0,
            behavior: "smooth"
        })
    })
}

/**
 * 更多商品
 */
function getMore() {
    $(".morecarts").on("click", () => {
        let findname = JSON.parse(localStorage.getItem("findname"));
        findname.start += 40;
        localStorage.setItem("findname", JSON.stringify(findname));
        getAjax()
    })
}

/**
 * 价格最低
 */
function orderChange() {
    //从本地获取数据
    $(".listchange>ul").on("click", "li", e => {
        e = e || window.event;
        let target = e.target;
        // 价格最低
        if (target.innerText == "价格最低") {
            let findname = JSON.parse(localStorage.getItem("findname"));
            findname.minPrice = "minPrice";
            localStorage.setItem("findname", JSON.stringify(findname));
            //切换样式
            $('.listchange>ul>li').removeClass()
            $(target).addClass("active1");
        }
        else if(target.innerText == "销量最高"){
            let findname = JSON.parse(localStorage.getItem("findname"));
            findname.sort= 2;
            localStorage.setItem("findname", JSON.stringify(findname));
            //切换样式
            $('.listchange>ul>li').removeClass()
            $(target).addClass("active1");
        }
        getAjax()
    })
}

/**
 * 点击显示详情页面
 */

 function goodsDetails(id) {
    location.href = "../goodsDetails.html?" + id
  }
  